{*******************************************************}
{                                                       }
{       MNEdit                                          }
{                                                       }
{       Copyright (C) 2014 Mohammed Naman               }
{                                                       }
{       This is a simple TEdit class with extra         }
{        properties which I use in my applications      }
{       Version : 0.2                                   }
{                                                       }
{    What's New                                         }
{      0.1 : initial version - 19-09-2014               }
{      0.2 : Addin EnterAsTab property - 20-09-2014     }
{                                                       }
{*******************************************************}

unit MNEdit;

interface

uses
  SysUtils, Classes, Controls, StdCtrls,Windows,Messages;

type
  TMNEdit = class(TEdit)
  private
    FMinLength: Byte;
    FStartWith: string;
    FEnterAsTab: Boolean;
    procedure SetMinLength(const Value: Byte);
    procedure SetStartWith(const Value: string);
    procedure SetEnterAsTab(const Value: Boolean);
    { Private declarations }
  protected
    procedure DoEnter;override;
    procedure DoExit;override;
    procedure KeyPress(var Key: Char); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property StartWith:string read FStartWith write SetStartWith;
    property MinLength:Byte read FMinLength write SetMinLength default 0;
    property EnterAsTab:Boolean read FEnterAsTab write SetEnterAsTab;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('MN Controls', [TMNEdit]);
end;

{ TMNEdit }

constructor TMNEdit.Create(AOwner: TComponent);
begin
  inherited;
end;

procedure TMNEdit.DoEnter;
var
  StartWithLen,TextLen : Integer;
begin
  StartWithLen := Length(FStartWith);
  TextLen := Length(Text);

  if TextLen = 0 then
    Text := FStartWith;

  if TextLen =  StartWithLen then
    SelStart := StartWithLen
  else if TextLen > StartWithLen then
  begin
    SelStart := StartWithLen;
    SelLength := TextLen - StartWithLen;
  end;
  inherited;
end;

procedure TMNEdit.DoExit;
begin
  if CompareText(FStartWith,Copy(Text,1,Length(FStartWith))) <> 0 then
  begin
    Text := FStartWith;
    SelStart := Length(FStartWith)+1;
    SetFocus;
  end;

  if (FMinLength > 0) and (Length(Text) < FMinLength) then
    SetFocus;
  inherited;
end;

procedure TMNEdit.KeyPress(var Key: Char);
begin
  if FEnterAsTab and  (key = #13) then
  begin
    PostMessage(Handle, wm_KeyDown, vk_Tab, 0 );
    Key := #0;
  end
  else
    inherited;
end;

procedure TMNEdit.SetEnterAsTab(const Value: Boolean);
begin
  FEnterAsTab := Value;
end;

procedure TMNEdit.SetMinLength(const Value: Byte);
begin
  if value > MaxLength then
    raise Exception.Create('Min Length should be less or equal MaxLength');

  FMinLength := Value;
end;

procedure TMNEdit.SetStartWith(const Value: string);
begin
  FStartWith := Value;
  Text := FStartWith;
end;

end.
